{% extends "base.html" %}
{% block extra_css %}
<style>
    /* 管理页面容器 */
    .manage-container {
        max-width: 800px;
        margin: 2rem auto;
        padding: 2rem;
        background: var(--card-bg);
        border-radius: var(--border-radius);
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        border: 1px solid rgba(255, 255, 255, 0.1);
    }

    .manage-title {
        color: var(--accent);
        font-size: 1.8rem;
        margin-bottom: 1.5rem;
        padding-bottom: 1rem;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }

    .section-title {
        color: var(--accent);
        font-size: 1.4rem;
        margin: 2rem 0 1rem;
    }

    /* 成员列表样式 */
    .members-list {
        background: rgba(255, 255, 255, 0.03);
        border-radius: var(--border-radius);
        padding: 1.5rem;
        margin-bottom: 2rem;
    }

    .member-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1rem;
        margin-bottom: 0.75rem;
        background: rgba(255, 255, 255, 0.05);
        border-radius: var(--border-radius);
        transition: var(--transition);
    }

    .member-item:last-child {
        margin-bottom: 0;
    }

    .member-name {
        font-weight: 500;
        color: var(--light);
    }

    /* 按钮样式 */
    .btn {
        padding: 0.5rem 1rem;
        border-radius: var(--border-radius);
        font-weight: 600;
        cursor: pointer;
        transition: var(--transition);
        text-decoration: none;
        display: inline-block;
        margin-right: 0.5rem;
        margin-bottom: 0.5rem;
    }

    .remove-btn {
        background: var(--warning);
        color: white;
        border: none;
    }

    .remove-btn:hover {
        background: #e01e6b;
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(247, 37, 133, 0.3);
    }

    .danger-btn {
        background: var(--warning);
        color: white;
        border: none;
        padding: 0.75rem 1.5rem;
    }

    .danger-btn:hover {
        background: #e01e6b;
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(247, 37, 133, 0.3);
    }

    .primary-btn {
        background: var(--primary);
        color: white;
        border: none;
        padding: 0.75rem 1.5rem;
    }

    .primary-btn:hover {
        background: var(--secondary);
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(67, 97, 238, 0.3);
    }

    .outline-btn {
        background: transparent;
        border: 2px solid var(--primary);
        color: var(--primary);
        padding: 0.75rem 1.5rem;
    }

    .outline-btn:hover {
        background: var(--primary);
        color: white;
    }

    /* 表单样式 */
    .form-group {
        margin-bottom: 1.5rem;
    }

    .form-group label {
        display: block;
        margin-bottom: 0.5rem;
        font-weight: 600;
        color: var(--light);
    }

    .form-control {
        width: 100%;
        padding: 0.75rem 1rem;
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: var(--border-radius);
        color: var(--light);
        font-size: 1rem;
        transition: var(--transition);
    }

    .form-control:focus {
        outline: none;
        border-color: var(--primary);
        box-shadow: 0 0 0 2px rgba(67, 97, 238, 0.2);
    }

    textarea.form-control {
        min-height: 100px;
        resize: vertical;
    }

    /* 操作按钮组 */
    .action-buttons {
        margin-top: 2rem;
        display: flex;
        flex-wrap: wrap;
        gap: 1rem;
    }

    /* 空状态提示 */
    .empty-state {
        color: var(--gray);
        text-align: center;
        padding: 1rem;
    }

    /* 响应式调整 */
    @media (max-width: 768px) {
        .manage-container {
            padding: 1.5rem;
            margin: 1rem;
        }

        .manage-title {
            font-size: 1.5rem;
        }

        .member-item {
            flex-direction: column;
            align-items: flex-start;
            gap: 0.75rem;
        }

        .action-buttons {
            flex-direction: column;
        }

        .btn {
            width: 100%;
            margin-right: 0;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="manage-container">
    <h2 class="manage-title">管理房间: {{ room_name }}</h2>

    <h3 class="section-title">房间成员</h3>
    <div class="members-list" id="members-list" data-room-id="{{ room_id }}">
        {% for member in members %}
        <div class="member-item">
            <span class="member-name">{{ member.0 }}</span>
            <button class="btn remove-btn remove-user-btn" data-user-id="{{ member.1 }}">移除用户</button>
        </div>
        {% empty %}
        <div class="empty-state">暂无其他成员</div>
        {% endfor %}
    </div>

    <h3 class="section-title">更新房间信息</h3>
    <form method="post">
        {% csrf_token %}
        <div class="form-group">
            <label>房间名称</label>
            {{ form.name }}
        </div>
        <div class="form-group">
            <label>房间描述</label>
            {{ form.description }}
        </div>
        <button type="submit" class="primary-btn">更新房间信息</button>
    </form>

    <div class="action-buttons">
        <a href="{% url 'Chat:delete_room' room_id=room_id %}" class="danger-btn">删除房间</a>
        <a href="{% url 'Chat:room_list' %}" class="outline-btn">返回房间列表</a>
    </div>
</div>

<script>
document.getElementById('members-list').addEventListener('click', event => {
    if (event.target && event.target.classList.contains('remove-user-btn')) {
        if (!confirm('确定要移除此用户吗？此操作无法撤销。')) {
            return;
        }

        const userId = event.target.getAttribute('data-user-id');
        const roomId = document.getElementById('members-list').getAttribute('data-room-id');

        const data = new FormData();
        data.append('user_id', userId);
        data.append('room_id', roomId);

        fetch(`/chat/${userId}/${roomId}/block/`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': '{{ csrf_token|escapejs }}',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: data
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                event.target.closest('.member-item').remove();
                // 可以替换为更美观的通知方式
                showNotification(data.message, 'success');
            } else {
                showNotification(data.error || '操作失败', 'error');
            }
        })
        .catch(err => {
            console.error(err);
            showNotification('网络错误，请重试', 'error');
        });
    }
});

function showNotification(message, type) {
    // 这里可以替换为您喜欢的通知方式
    const notification = document.createElement('div');
    notification.className = `notification ${type}`;
    notification.textContent = message;
    document.body.appendChild(notification);

    setTimeout(() => {
        notification.classList.add('fade-out');
        setTimeout(() => notification.remove(), 500);
    }, 3000);
}
</script>
{% endblock %}